﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Type2Diabetes
{
    public class dbFunctions
    {
        private DBContext dbContext;
        private ObservableCollection<Person> _person;
        public ObservableCollection<Person> person
        {
            get
            {
                return _person;
            }
            set
            {
                if (_person != value)
                {
                    _person = value;
                }
            }
        }
        private ObservableCollection<Doctor> _doctor;
        public ObservableCollection<Doctor> doctor
        {
            get
            {
                return _doctor;
            }
            set
            {
                if (_doctor != value)
                {
                    _doctor = value;
                }
            }
        }
        private ObservableCollection<Food> _food;
        public ObservableCollection<Food> food
        {
            get
            {
                return _food;
            }
            set
            {
                if (_food != value)
                {
                    _food = value;
                }
            }
        }
        private ObservableCollection<FoodHist> _foodhist;
        public ObservableCollection<FoodHist> foodhist
        {
            get
            {
                return _foodhist;
            }
            set
            {
                if (_foodhist != value)
                {
                    _foodhist = value;
                }
            }
        }
        private ObservableCollection<GlucoseGoal> _glucosegoal;
        public ObservableCollection<GlucoseGoal> glucosegoal
        {
            get
            {
                return _glucosegoal;
            }
            set
            {
                if (_glucosegoal != value)
                {
                    _glucosegoal = value;
                }
            }
        }
        private ObservableCollection<GlucoseHist> _glucosehist;
        public ObservableCollection<GlucoseHist> glucosehist
        {
            get
            {
                return _glucosehist;
            }
            set
            {
                if (_glucosehist != value)
                {
                    _glucosehist = value;
                }
            }
        }
        private ObservableCollection<Devices> _devices;
        public ObservableCollection<Devices> devices
        {
            get
            {
                return _devices;
            }
            set
            {
                if (_devices != value)
                {
                    _devices = value;
                }
            }
        }
        public dbFunctions()
        {
            dbContext = new DBContext(DBContext.DBConnectionString);
        }

        public int insert_into_person(int personID, string fname, string lname, string gender,
            DateTime dateofbirth, float height, float weight, float idealweight,
            DateTime diagnosisdate, float diabtype)
        {
            person = new ObservableCollection<Person>();
            Person user = new Person
            {
                PID = personID,
                diabtype = diabtype,
                diadate = diagnosisdate,
                dob = dateofbirth,
                fname = fname,
                gen = gender,
                ht = height,
                idealwt = idealweight,
                lname = lname,
                wt = weight
            };
            person.Add(user);
            dbContext.person.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }
        public int get_person_info()
        {
            int does_exist = 0;
            var person = from Person user in dbContext.person
                         select user;
            foreach (var user in person)
            {
                does_exist += 1;
            }
            return does_exist;
        }

        public int insert_into_doctor(int doctorID, string docfname, string doclname, string docoffname, string docemail, int docph, string docrole)
        {
            doctor = new ObservableCollection<Doctor>();
            Doctor user = new Doctor
            {
                docid = doctorID,
                docfname = docfname,
                doclname = doclname,
                docoffname = docoffname,
                docemail = docemail,
                docph = docph,
                docrole = docrole
            };
            doctor.Add(user);
            dbContext.doctor.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }

        public int insert_into_devices(int deviceID, string deviceName, string deviceManufacturer)
        {
            devices = new ObservableCollection<Devices>();
            Devices user = new Devices
            {
                devid = deviceID,
                devname = deviceName,
                manu = deviceManufacturer
            };
            devices.Add(user);
            dbContext.devices.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }

        public int insert_into_glucosehist(string ghID, int personID, int deviceID, float glucoseValue, DateTime datetime, string ghNote, string whereMeasured)
        {
            glucosehist = new ObservableCollection<GlucoseHist>();
            GlucoseHist user = new GlucoseHist
            {
                GlucoseHistID = ghID,
                PID = personID,
                DID = deviceID,
                GValue = glucoseValue,
                DT = datetime,
                GlucoseHistNotes = ghNote,
                WM = whereMeasured
            };
            glucosehist.Add(user);
            dbContext.GlucoseHist.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }

        public int insert_into_glucosegoal(int ggID, int personID, float gLowValue, float gHighValue, float gGValue)
        {
            glucosegoal = new ObservableCollection<GlucoseGoal>();
            GlucoseGoal user = new GlucoseGoal
            {
                GGID = ggID,
                PID = personID,
                GLV = gLowValue,
                GHV = gHighValue,
                GGV = gGValue
            };
            glucosegoal.Add(user);
            dbContext.GlucoseGoal.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }

        public int insert_into_food(int foodID, string foodName, float serveSize, string serveSizeUnit, float calory, float carbon, float protein, float fat)
        {
            food = new ObservableCollection<Food>();
            Food user = new Food
            {
                fid = foodID,
                foodname = foodName,
                ss = serveSize,
                ssu = serveSizeUnit,
                calps = calory,
                carps = carbon,
                fps = protein,
                pps = fat
            };
            food.Add(user);
            dbContext.food.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;
        }

        public int insert_into_foodhist(int fhID, int foodID, DateTime datetime, float quantity, string fhNotes, int personID)
        {
            foodhist = new ObservableCollection<FoodHist>();
            FoodHist user = new FoodHist
            {
                fhid = fhID,
                foodid = foodID,
                datetime = datetime,
                qty = quantity,
                fhnotes = fhNotes,
                pid = personID
            };
            foodhist.Add(user);
            dbContext.foodhistory.InsertOnSubmit(user);
            dbContext.SubmitChanges();
            return 0;        
        }
    }
}
